package dataType

import (
	"fmt"
	"strconv"
	"unsafe"
)

/** 认识数据类型 */

/*
*
有符号整型(包含正，负值)： int8, int16, int32, int64, int
*/
func intCase() {
	var n1 int = 2
	n2 := 4
	fmt.Println("n1 = ", n1, "; n2 = ", n2)
	fmt.Printf("type %T, n1 长度 %d \n", n1, unsafe.Sizeof(n1)) // a 的类型和大小
	fmt.Printf("type %T, n2 长度 %d \n", n2, unsafe.Sizeof(n2)) // a 的类型和大小
}

/*
*无符号整型(只有正数)：uint8, uint16, uint32, uint64, uint
 */
func uintCase() {

	var n1 uint = 2
	var n2 uint = 4
	fmt.Println("n1 = ", n1, "; n2 = ", n2)
}

/*
 * 浮点型：float32：32 位浮点数float64：64 位浮点数
 */
func floatCase() {
	var price1 float32 = 99.08
	price2 := 89.99
	fmt.Println("price1 = ", price1, "; price2 = ", price2)
}

/*
*
复数类型
*/
func complexCase() {
	c := complex(1, 2)
	fmt.Println(c, "复数实部=", real(c), "复数虚部=", imag(c))
}

/*
*
布尔类型bool
*/
func boolCase() {
	var sex bool = false
	b := true
	fmt.Println("sex =", sex)
	fmt.Println("b = ", b)
}

/*
*
字符串类型
*/
func stringCase() {

	var name string = "LiLi"
	name2 := "丽丽"
	nameas := name + "-" + name2
	fmt.Println("name = ", name)
	fmt.Println("name2 = ", name2)
	fmt.Println("nameas = ", nameas)
}

/*
* 类型转换案例
 */
func typeConvertCase() {
	var num1 int = 25
	str1, str2 := "31", "34.09"
	var price1 float32 = 99.08
	sex1 := true

	fmt.Println(num1, str1, str2, price1, sex1)

	// int 转字符串
	s1 := strconv.Itoa(num1)
	fmt.Println("int 转字符串：", s1)
	s2 := fmt.Sprintf("%d", num1)
	fmt.Println("int 转字符串：", s2)

	// 字符串转 int
	ss1, err1 := strconv.Atoi(str1)
	if err1 != nil {
		fmt.Println("转换错误...")
	}
	fmt.Println("字符串转 int", ss1)

	// float 转 int
	var nn1 int = int(price1)
	fmt.Println("float 转 int ", nn1)

	// bool 转 string
	ss2 := strconv.FormatBool(sex1)
	fmt.Println("bool 转 string ", ss2)

}

func Test() {
	intCase()

	uintCase()

	floatCase()

	complexCase()

	boolCase()

	stringCase()

	typeConvertCase()
}
